package hw_7_25.loginSimulation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class loginSimulation {
    static Random r = new Random();
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("请输入用户名：");
        String userName = sc.next();
        System.out.println("请输入密码：");
        String passWord = sc.next();
        System.out.println();
        System.out.println("请输入验证码的位数：");
        int length = sc.nextInt();
        boolean isAccountCorrect = checkAccountInfo(userName, passWord);
        String captcha = generateCaptcha(length);
        System.out.println("请输入产生的验证码：" + captcha);
        String inputCaptcha = sc.next();
        boolean isCaptchaCorrect = checkCaptchaInfo(captcha, inputCaptcha);
        boolean login = login(isAccountCorrect, isCaptchaCorrect);
        if (login){
            System.out.println("验证通过，请使用本系统！");
        } else {
            System.out.println("验证未通过，拒绝使用！");
        }
    }
    public static boolean checkAccountInfo(String userName,String passWord){
        if (userName.equals("admin") && passWord.equals("password")){
            return true;
        }
        return false;
    }

    public static String generateCaptcha(int length){
        List<Character> charList = new ArrayList<>();
        for (char c = '0'; c <= '9'; c++) {
            charList.add(c);
        }
        for (char c = 'a'; c <= 'z'; c++) {
            charList.add(c);
        }
        for (char c = 'A'; c <= 'Z'; c++) {
            charList.add(c);
        }
        int randomIndex = -1;
        StringBuilder codeBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            randomIndex =  r.nextInt(charList.size());
            Character ch = charList.get(randomIndex);
            codeBuilder.append(ch);
        }
        String codeStr = codeBuilder.toString();
        return codeStr;
    }

    public static boolean checkCaptchaInfo(String captcha,String inputCaptcha){
        if (captcha.equals(inputCaptcha)){
            return true;
        }
        return false;
    }
    public static boolean login(boolean isAccountCorrect,boolean isCaptchaCorrect){
        if (isAccountCorrect && isCaptchaCorrect){
            return true;
        }
        return false;
    }
}
